package cn.ncu.wechat.util;


import java.util.Map;

public class ConfigCheckUtil {
    public static String validateConfig(Map<String, Object> configMap) {
        try {
            // 基本信息验证
            Map<String, Object> basic = (Map<String, Object>) configMap.get("basic");
            if (basic == null || !basic.containsKey("name") || !basic.containsKey("appId") || !basic.containsKey("appSecret") || !basic.containsKey("workWxKf")) {
                return "基础信息不完整";
            }
            if (basic.get("name").toString().isEmpty() || basic.get("appId").toString().isEmpty() ||
                    basic.get("appSecret").toString().isEmpty() || basic.get("workWxKf").toString().isEmpty()) {
                return "基础信息字段不能为空";
            }

            // 可选的 Logo 检查，忽略即可
            if (basic.containsKey("logo") && basic.get("logo").toString().isEmpty()) {
                return "如果提供了 Logo，不能为空";
            }

            // 主题信息验证
            Map<String, Object> theme = (Map<String, Object>) configMap.get("theme");
            if (theme == null || !theme.containsKey("primaryColor") || !theme.containsKey("backgroundColor")) {
                return "主题信息不完整";
            }
            if (theme.get("primaryColor").toString().isEmpty() || theme.get("backgroundColor").toString().isEmpty()) {
                return "主题的 primaryColor 和 backgroundColor 不能为空";
            }

            // 可选的 backgroundImage 检查，忽略即可
            if (theme.containsKey("backgroundImage") && theme.get("backgroundImage").toString().isEmpty()) {
                return "如果提供了背景壁纸，不能为空";
            }

            // 启动配置验证
            Map<String, Object> launch = (Map<String, Object>) configMap.get("launch");
            if (launch == null || !launch.containsKey("enabled") || !launch.containsKey("image") || !launch.containsKey("duration") || !launch.containsKey("enableLink")) {
                return "启动页信息不完整";
            }
            if (launch.get("image").toString().isEmpty()) {
                return "启动页图片不能为空";
            }
            if (Integer.parseInt(launch.get("duration").toString()) <= 0) {
                return "启动页显示时长必须大于 0";
            }

            // 跳转链接配置
            if (launch.containsKey("link")) {
                Map<String, Object> link = (Map<String, Object>) launch.get("link");
                if (!link.containsKey("type") || !link.containsKey("target")) {
                    return "跳转链接信息不完整";
                }
                String type = link.get("type").toString();
                if (!type.equals("page") && !type.equals("article") && !type.equals("url")) {
                    return "跳转类型必须是 'page'、'article' 或 'url' 中的一个";
                }
                if (link.get("target").toString().isEmpty()) {
                    return "跳转目标不能为空";
                }
            }

            // 支付信息验证
            Map<String, Object> payment = (Map<String, Object>) configMap.get("payment");
            if (payment == null || !payment.containsKey("wxpayEnabled") || !payment.containsKey("wxpayAppId") || !payment.containsKey("wxpayMchId") ||
                    !payment.containsKey("wxpayApiKey") || !payment.containsKey("wxpayApiV3Key")) {
                return "支付信息不完整";
            }
            if (!Boolean.parseBoolean(payment.get("wxpayEnabled").toString())) {
                return "如果启用了支付，wxpayEnabled 必须为 true";
            }
            if (payment.get("wxpayAppId").toString().isEmpty() || payment.get("wxpayMchId").toString().isEmpty() ||
                    payment.get("wxpayApiKey").toString().isEmpty() || payment.get("wxpayApiV3Key").toString().isEmpty()) {
                return "支付字段不能为空";
            }

            // 如果通过所有验证
            return "配置有效";

        } catch (Exception e) {
            return "无效的配置结构: " + e.getMessage();
        }
    }
}
